package com.bigdata.movieticketapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.entity.CancelOrder;
import com.bigdata.movieticketapi.entity.MovieOrder;
import com.bigdata.movieticketapi.entity.User;
import com.bigdata.movieticketapi.mapper.CancelOrderMapper;
import com.bigdata.movieticketapi.mapper.MovieOrderMapper;
import com.bigdata.movieticketapi.mapper.UserMapper;
import com.bigdata.movieticketapi.service.ICancelOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.vo.req.AuditReqVO;
import com.bigdata.movieticketapi.vo.req.CancelOrderReqVO;
import com.bigdata.movieticketapi.vo.req.PageCancelOrderReqVO;
import com.bigdata.movieticketapi.vo.resp.PageCancelOrderRespVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 记录取消订单 服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-11-15
 */
@Service
public class CancelOrderServiceImpl extends ServiceImpl<CancelOrderMapper, CancelOrder> implements ICancelOrderService {

    @Resource
    private CancelOrderMapper cancelOrderMapper;

    @Resource
    private MovieOrderMapper movieOrderMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public void cancelOrder(CancelOrderReqVO vo) {
        CancelOrder cancelOrder = new CancelOrder();
        cancelOrder.setOrderId(vo.getOrderId());
        cancelOrder.setStatus(1);
        cancelOrder.setReason(vo.getReason());
        int result = cancelOrderMapper.insert(cancelOrder);
        if (result!=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Transactional
    @Override
    public void audit(AuditReqVO vo) {
        CancelOrder cancelOrder = cancelOrderMapper.selectById(vo.getId());
        cancelOrder.setStatus(vo.getStatus());
        int result = cancelOrderMapper.updateById(cancelOrder);
        if (result!=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
        if (vo.getStatus() == 2){
            MovieOrder movieOrder = movieOrderMapper.selectById(cancelOrder.getOrderId());
            movieOrder.setIsCancel(1);
            User user = userMapper.selectById(movieOrder.getUserId());
            BigDecimal oldWallet = user.getWallet();
            user.setWallet(oldWallet.add(movieOrder.getPrice()));
            int update = movieOrderMapper.updateById(movieOrder);
            if (update!=1){
                throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
            }
            int updateUser = userMapper.updateById(user);
            if (updateUser!=1){
                throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
            }

        }
    }

    @Override
    public PageResult<PageCancelOrderRespVO> pageCancelOrder(PageCancelOrderReqVO vo) {
        IPage<PageCancelOrderRespVO> result = cancelOrderMapper.selectPageCancelOrder(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(),result.getTotal());
    }
}
